package com.sdy.healthcode.h5.controller.user;

import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.dto.userpatient.UserPatientDTO;
import com.sdy.healthcode.biz.model.User;
import com.sdy.healthcode.biz.model.UserPatient;
import com.sdy.healthcode.biz.service.UserPatientService;
import com.sdy.healthcode.biz.service.UserService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/userPatient")
@Api(tags = "就诊人")
public class UserPatientController extends BaseController {
    @Autowired
    private UserPatientService userPatientService;
    @Autowired
    private UserService userService;

    @GetMapping("/get")
    @ApiOperation(value = "获取就诊人信息")
    @ApiImplicitParam(value = "id", name = "id")
    public Response get(HttpServletRequest request, Integer id) throws BizException {
        UserPatient userPatient = userPatientService.getById(id);
        Assert.isNull(userPatient, "就诊人不存在");
        Assert.isTrue(!userPatient.getUserId().equals(getUserId(request)), "没有查看权限");
        return Response.success(userPatient);
    }
    
    @PostMapping("/save")
    @ApiOperation(value = "添加就诊人")
    public Response save(HttpServletRequest request, @RequestBody UserPatientDTO userPatientDTO) throws BizException {
        userPatientService.addUserPatient(getUserId(request), userPatientDTO);
        return Response.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新就诊人")
    public Response update(HttpServletRequest request, @RequestBody UserPatientDTO userPatientDTO) throws BizException {
        userPatientService.updateUserPatient(getUserId(request), userPatientDTO);
        return Response.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除就诊人")
    public Response delete(HttpServletRequest request, @RequestBody UserPatientDTO userPatientDTO) throws BizException {
        userPatientService.removeUserPatient(getUserId(request), userPatientDTO.getUserPatientId());
        return Response.success();
    }
    
    @GetMapping("/list")
    @ApiOperation(value = "获取就诊人列表")
    public Response<List<UserPatientDTO>> list(HttpServletRequest request) {
        Integer userId = getUserId(request);
        List<UserPatientDTO> userPatients = userPatientService.listByUserId(userId);
        // 添加当前用户
        User user = userService.getById(userId);
        userPatients.add(0, new UserPatientDTO().setName(user.getName()).setCardId(user.getCardId()).setSelf(true));
        return Response.success(userPatients);
    }
}
